package com.api.workflow.web;


import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

@Path("/oa/function")
public class FunctionController {

    @POST
    @Path("/initiatePurchase")
    @Produces(MediaType.APPLICATION_JSON)
    public String initiatePurchase(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        HashMap<String, Object> param = new HashMap<>();
        BaseBean baseBean = new BaseBean();
        try{
            param.put("api_status", true);
            Map<String, Object> parameterMap = ParamUtil.request2Map(request);
            String ids = String.valueOf(parameterMap.get("ids"));
            if (ids == null || ids.length() == 0){
                param.put("api_msgcode","1");
                param.put("api_msg","数据错误");
            }else {
                if (ids.endsWith(",")){
                    ids = ids.substring(0,ids.length()-1);
                }
                String sql = "select cgsqpzlx from uf_cgsqd where id in ("+ ids +")";
                baseBean.writeLog("FunctionController initiatePurchase sql:"+ sql);
                RecordSet rs = new RecordSet();
                rs.executeQuery(sql);
                HashSet<String> set = new HashSet<>();
                while (rs.next()){
                    set.add(rs.getString("cgsqpzlx"));
                }

                if (set.size() > 1){
                    param.put("api_msgcode","1");
                    param.put("api_msg","采购申请凭证类型不一致");
                }else {
                    param.put("api_msgcode","0");
                    param.put("api_msg","成功");
                }
            }
        }catch (Exception e){
            baseBean.writeLog("FunctionController initiatePurchase error:"+ e);
        }
        return JSONObject.toJSONString(param);
    }
}
